package com.zadtek.sharkRider.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import cn.jpush.android.api.JPushInterface;

import com.zadtek.sharkRider.activity.MainActivity;
import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.activity.data.PushUserData;
import com.zadtek.sharkRider.activity.data.QiangDanData;
import com.zadtek.sharkRider.frame.JsonProcessHelper;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "JPush";

	private NotificationManager nm;

	private String uid;

	@Override
	public void onReceive(Context context, Intent intent) {
		if (null == nm) {
			nm = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);

		}
		Bundle bundle = intent.getExtras();

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle
					.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			// Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
			// send the Registration Id to your server...

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
				.getAction())) {
			// 自定义消息
			processCustomMessage(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
				.getAction())) {
			// 通知消息
			// String title = bundle.getString(JPushInterface.EXTRA_TITLE);
			// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
			// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
			// NotificationHelper.showMessageNotification(context, nm, title,
			// message, extras, bundle, "");

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
				.getAction())) {
			// Log.d(TAG, "[MyReceiver] 用户点击打开了通知");

		}
	}

	// send msg to MenuTabActivity
	private void processCustomMessage(Context context, Bundle bundle) {
		String title = bundle.getString(JPushInterface.EXTRA_TITLE);
		String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

		if (extras != null) {
			PushUserData pushData = JsonProcessHelper
					.jsonProcess_getPushData(extras);
			if (pushData != null) {
				String type = pushData.getQs_type();
				String authorid = pushData.getXyrr_qsid();
				SharedPreferences sp = context.getSharedPreferences(
						Const.PREF_NAME, Context.MODE_PRIVATE);
				uid = sp.getString("uid", "");

				NotificationHelper.showMessageNotification(context, nm, title,
						message, extras, bundle, type);

				// 获取数量
				initArrays(new Handler() {
					@Override
					public void handleMessage(Message msg) {
						if (!"emp".equals(msg.obj.toString())) {
							QiangDanData sumcout = (QiangDanData) msg.obj;
							String xincount = sumcout.getNeworder();
							String noget = sumcout.getNoget();
							String nosend = sumcout.getNosend();
							if (MainActivity.radio_goqh != null) {
								MainActivity.radio_goqh.setText("待取货(" + noget
										+ ")");
							}
							if (MainActivity.radio_gops != null) {
								MainActivity.radio_gops.setText("配送中(" + nosend
										+ ")");
							}
						} else {
						}
					}
				});

			}
		}

	}

	// 获取数量
	private void initArrays(final Handler handler) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					QiangDanData datas = JsonProcessHelper
							.jsonProcess_getOrderCount("get_order_count", uid);
					if (datas != null && datas.getStatus().equals("1")) {
						handler.obtainMessage(0, datas).sendToTarget();
					} else {
						handler.obtainMessage(0, "emp").sendToTarget();
					}
				} catch (Exception e) {
					Thread.interrupted();
					e.printStackTrace();
				}

			}
		}).start();
	}

}
